package com.example.team.dao.Imp;

import com.example.team.dao.IVehicleDao;
import com.example.team.entity.Vehicle;
import com.example.team.entity.VehicleUsage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：蓝江富
 * 2023年10月29日20:10:44
 */
@Repository
public class IVehicleDaoImpl implements IVehicleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private IVehicleUsageDaoImpl vehicleUsageDao;
    @Override
    public int insertVehicle(Vehicle vehicle) {
        return jdbcTemplate.update(
                "insert into vehicle(" +
                        "vehicleId,license,vehicleType,purchaseDate,warrantyDate,isUnUsed" +
                        ") " +
                        "values(?,?,?,?,?,?)"
                ,vehicle.getVehicleId()
                ,vehicle.getLicense()
                ,vehicle.getVehicleType()
                ,vehicle.getPurchaseDate()
                ,vehicle.getWarrantyDate()
                ,vehicle.getIsUnUsed()
        );
    }

    @Override
    public int deleteVehicle(Vehicle vehicle) {
        //先将关联表的对应数据外键设置为null
        jdbcTemplate.update("update vehicle_usage set " +
                        "vehicleId=null where vehicleId=?"
                        ,vehicle.getVehicleId());
        return jdbcTemplate.update(
                "delete from vehicle " +
                "where vehicleId=?",vehicle.getVehicleId());
    }

    @Override
    public int updateVehicle(Vehicle vehicle) {
        //如果更新的字段为空，则保留旧数据
        Vehicle getV=getVehicleById(vehicle);
        if(vehicle.getLicense()==null)
            vehicle.setLicense(getV.getLicense());
        if(vehicle.getVehicleType()==null)
            vehicle.setVehicleType(getV.getVehicleType());
        if(vehicle.getPurchaseDate()==null)
            vehicle.setPurchaseDate(getV.getPurchaseDate());
        if(vehicle.getWarrantyDate()==null)
            vehicle.setWarrantyDate(getV.getWarrantyDate());
        if(vehicle.getIsUnUsed()==null)
            vehicle.setIsUnUsed(getV.getIsUnUsed());
        return jdbcTemplate.update("update vehicle set " +
                "license=?,vehicleType=?,purchaseDate=?,warrantyDate=?,isUnUsed=? where vehicleId=?"
                ,vehicle.getLicense()
                ,vehicle.getVehicleType()
                ,vehicle.getPurchaseDate()
                ,vehicle.getWarrantyDate()
                ,vehicle.getIsUnUsed()
                ,vehicle.getVehicleId()
        );
    }

    @Override
    public List<Vehicle> getAllVehicle(Vehicle vehicle) {
        if(vehicle.getLicense()!=null)
            return jdbcTemplate.query("select " +
                "vehicleId,license,vehicleType,purchaseDate,warrantyDate,isUnUsed " +
                "from vehicle where license='"+vehicle.getLicense()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(Vehicle.class));
        else
            return jdbcTemplate.query("select " +
                "vehicleId,license,vehicleType,purchaseDate,warrantyDate,isUnUsed " +
                "from vehicle",new Object[]{},
                new BeanPropertyRowMapper<>(Vehicle.class));
    }

    @Override
    public Vehicle getVehicle(Vehicle vehicle) {
        List<Vehicle> result=jdbcTemplate.query("select " +
                                "vehicleId,license,vehicleType,purchaseDate,warrantyDate,isUnUsed " +
                                "from vehicle where license='"+vehicle.getLicense()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Vehicle.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public Vehicle getVehicleById(Vehicle vehicle) {
        List<Vehicle> result=jdbcTemplate.query("select " +
                                "vehicleId,license,vehicleType,purchaseDate,warrantyDate,isUnUsed " +
                                "from vehicle where vehicleId='"+vehicle.getVehicleId()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Vehicle.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
